package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.modules.buss.entity.BussCameraInfoEntity;
import io.gbznt.modules.buss.service.BussCameraInfoService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.entity.SysDictEntity;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import io.gbznt.modules.sys.service.SysDictService;
import io.gbznt.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussFarmlandInfoDao;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuInfoEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussRtuInfoService;

@Service("bussFarmlandInfoService")
public class BussFarmlandInfoServiceImpl extends ServiceImpl<BussFarmlandInfoDao, BussFarmlandInfoEntity>
		implements BussFarmlandInfoService {

	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private BussRtuInfoService bussRtuInfoService;
	@Autowired
	private BussCameraInfoService bussCameraInfoService;
	@Autowired
	private SysDeptService sysDeptService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String name = (String) params.get("name");
		// 所属基地
		String jd = (String) params.get("jd");

		IPage<BussFarmlandInfoEntity> page = this.page(new Query<BussFarmlandInfoEntity>().getPage(params),
				new QueryWrapper<BussFarmlandInfoEntity>().like(StringUtils.isNotEmpty(name), "name", name)
						.eq(StringUtils.isNotBlank(jd), "dept_id", jd)
						// 个人数据权限控制
						.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()), "dept_id",
								sysDeptService.getPermissionDeptIdsArr(true, true))
						.orderByAsc("order_num").orderByDesc("create_time"));

		for (BussFarmlandInfoEntity bussFarmlandInfoEntity : page.getRecords()) {
			// 所属基地
			SysDeptEntity dept = sysDeptService.getById(bussFarmlandInfoEntity.getDeptId());
			if (null != dept) {
				bussFarmlandInfoEntity.setFarmlandAreaName(dept.getName());
			}
			// 农作物
			SysDictEntity sysDictEntity = sysDictService.getById(bussFarmlandInfoEntity.getCropDict());
			if (null != sysDictEntity) {
				bussFarmlandInfoEntity.setCropDictName(sysDictEntity.getValue());
			}
			// 负责人
			SysUserEntity sysUserEntity = sysUserService.getById(bussFarmlandInfoEntity.getHandler());
			if (null != sysUserEntity) {
				bussFarmlandInfoEntity.setHandlerName(sysUserEntity.getName());
			}
			// 田块下是否有rtu
			QueryWrapper<BussRtuInfoEntity> qwRtu = new QueryWrapper<>();
			qwRtu.eq(StringUtils.isNotBlank(bussFarmlandInfoEntity.getId()), "farmland_info_id",
					bussFarmlandInfoEntity.getId());
			int count = bussRtuInfoService.count(qwRtu);
			if (count > 0) {
				bussFarmlandInfoEntity.setYnRtu(1);
			} else {
				bussFarmlandInfoEntity.setYnRtu(0);
			}

		}

		return new PageUtils(page);
	}

	/**
	 * 修改田块，且同步修改田块部门修改相应的RTU设备摄像头部门
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void uodateByid(BussFarmlandInfoEntity bussFarmlandInfo) {

		// 查询出该田块下所有的rtu，并修改部门
		QueryWrapper<BussRtuInfoEntity> rtu = new QueryWrapper<>();
		rtu.eq("farmland_info_id", bussFarmlandInfo.getId());
		List<BussRtuInfoEntity> rtuList = bussRtuInfoService.list(rtu);
		for (BussRtuInfoEntity bussRtuInfoEntity : rtuList) {
			bussRtuInfoEntity.setDeptId(bussFarmlandInfo.getDeptId());
			// 修改rtu部门
			bussRtuInfoService.updateById(bussRtuInfoEntity);
			// 修改设备部门

			Map<String, Object> map = new HashMap<>();
			map.put("deptId", bussFarmlandInfo.getDeptId());
			map.put("rtuId", bussRtuInfoEntity.getId());
			baseMapper.updateRtuEqByRtu(map);
		}

		// 查询出该田块下所有的摄像头，并修改部门
		QueryWrapper<BussCameraInfoEntity> sxt = new QueryWrapper<>();
		sxt.eq("farmland_info_id", bussFarmlandInfo.getId());
		List<BussCameraInfoEntity> sxtList = bussCameraInfoService.list(sxt);
		for (BussCameraInfoEntity bussCameraInfoEntity : sxtList) {
			bussCameraInfoEntity.setDeptId(bussFarmlandInfo.getDeptId());
			bussCameraInfoService.updateById(bussCameraInfoEntity);
		}

		// 修改田块信息
		this.updateById(bussFarmlandInfo);

	}
}
